package 
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxState;
	import org.flixel.FlxSprite;
	import pessoas.PessoaSugavel;
	import player.Salazar;
	
	/**
	 * ...
	 * @author Lucas Mello
	 */
	public class Money extends FlxSprite
	{
		[Embed(source = '../assets/money/money1.png')] public static var money1:Class;
		[Embed(source = '../assets/money/money2.png')] public static var money2:Class;
		[Embed(source = '../assets/money/colar.png')] public static var colar:Class;
		[Embed(source = '../assets/money/diamante.png')] public static var diamante:Class;
		[Embed(source = '../assets/money/gold.png')] public static var gold:Class;
		[Embed(source = '../assets/money/wallet.png')] public static var wallet:Class;
		
		public var playerSpr:player.Salazar;
		
		private var _maxVelocity:Number = 500;
		private var minVelocity:Number = 500;
		
		public function Money(x:int, y:int, type:int, p:player.Salazar)
		{
			playerSpr = p;
			
			var img:Class;
			switch (type)
			{
				case 0:
					img = null;
					break;
				case 1:
					img = wallet;
					break;
				case 2:
					img = colar;
					break;
				case 3:
					img = gold;
					break;
				case 4:
					img = diamante;
					break;					
			}
			
			super(x, y, img);
			
			if (type == 0)
			{
				loadGraphic(money1, true, false, 32, 32);
				addAnimation("float", [0, 1, 2, 3], 20, true);
				frame = Math.random() * 4;
				play("float");
			}
		}
		
		override public function update():void 
		{
			velocity.x = (playerSpr.x - x);
			velocity.y = (playerSpr.y - y);
			
			var magnitude:Number = Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
			
			if (magnitude > _maxVelocity)
			{
				velocity.x *= _maxVelocity / magnitude;
				velocity.y *= _maxVelocity / magnitude;
			}
			else if (magnitude < minVelocity)
			{
				velocity.x *= minVelocity / magnitude;
				velocity.y *= minVelocity / magnitude;
			}
			
			if (velocity.x > 0)
			{
				if (x + velocity.x*Main.delta > playerSpr.x)
				{
					x = playerSpr.x;
					velocity.x = 0;
				}
				/*else if (velocity.x > _maxVelocity)
					velocity.x = _maxVelocity;
				else if (velocity.x < minVelocity)
					velocity.x = minVelocity;*/
			}
			else
			{
				if (x + velocity.x*Main.delta < playerSpr.x)
				{
					x = playerSpr.x;
					velocity.x = 0;
				}
				/*else if (velocity.x < -_maxVelocity)
					velocity.x = -_maxVelocity;
				else if (velocity.x > -minVelocity)
					velocity.x = -minVelocity;*/
			}
			
			if (velocity.y > 0)
			{
				if (y + velocity.y*Main.delta > playerSpr.y)
				{
					y = playerSpr.y;
					velocity.y = 0;
				}
				/*else if (velocity.y > _maxVelocity)
					velocity.y = _maxVelocity;
				else if (velocity.y < minVelocity)
					velocity.y = minVelocity;*/
			}
			else
			{
				if (y + velocity.y*Main.delta < playerSpr.y)
				{
					y = playerSpr.y;
					velocity.y = 0;
				}
				/*else if (velocity.y < -_maxVelocity)
					velocity.y = -_maxVelocity;
				else if (velocity.y > -minVelocity)
					velocity.y = -minVelocity;*/
			}
			
			super.update();
		}
	}
}